package com.kobeliu.controller;

import com.kobeliu.pojo.bo.EditArticleBO;
import com.kobeliu.pojo.netty.ChatMsg;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.ChatMessageService;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("chat")
@Slf4j
public class ChatMessageController {

    @Autowired
    private ChatMessageService chatMessageService;

    @PostMapping("/saveMsg")
    public GraceJSONResult saveMsg(@RequestBody ChatMsg chatMsg){

        chatMessageService.saveMessage(chatMsg);

        return GraceJSONResult.ok();
    }

    @PostMapping("/getMyUnReadCounts")
    public GraceJSONResult getMyUnReadCounts(String myId){

        Map map = chatMessageService.getMyUnreadCounts(myId);
        return GraceJSONResult.ok(map);

    }

    //learMyUnReadCounts
    @PostMapping("/clearMyUnReadCounts")
    public GraceJSONResult clearMyUnReadCounts(String myId,String oppositeId){

        chatMessageService.clearMyUnReadCounts(myId,oppositeId);
        return GraceJSONResult.ok();

    }

    @PostMapping("/list/{userId}/{oppositeId}")
    public GraceJSONResult pagedChatMsg(@PathVariable String userId, @PathVariable String oppositeId,Integer page,Integer pageSize){

        if(page == null) page = 1;
        if(pageSize == null) pageSize = 10;

        PagedGridResult result = chatMessageService.pagedChatMsg(userId,oppositeId,page,pageSize);
        return GraceJSONResult.ok(result);

    }

    @PostMapping("/signRead/{msgId}")
    public GraceJSONResult signRead(@PathVariable("msgId") String msgId){

        chatMessageService.updateMsgSignRead(msgId);
        return GraceJSONResult.ok();

    }
}
